﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;

namespace CDoDNSessionScheduler
{
    /// <summary>
    /// Represents the sessions that a user has scheduled.
    /// </summary>
    public class UserSchedule
    {
        private const string isolatedStorageFilename = "userSchedule.txt";

        /// <summary>
        /// The tracks that the user has scheduled.
        /// The index of the array is the slot, and the value is the track.
        /// </summary>
        public int[] Tracks { get; set; }

        /// <summary>
        /// Creates an instance of the user's schedule class.
        /// </summary>
        /// <param name="totalSlots">The total number of slots available in the conference.</param>
        public UserSchedule(int totalSlots)
        {
            // create an instance of the tracks array
            Tracks = new int[totalSlots];

            // load the schedule (if it's been saved) from isolated storage
            string[] storedTracks = new string[0];
            string storedSchedule = string.Empty;
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(isolatedStorageFilename, FileMode.OpenOrCreate, isf))
                    using (StreamReader sr = new StreamReader(isfs))
                        storedSchedule = sr.ReadLine();
            if (!string.IsNullOrEmpty(storedSchedule))
                storedTracks = storedSchedule.Split(',');
            for (int i = 0; i < storedTracks.Length && i < Tracks.Length; i++)
                Tracks[i] = int.Parse(storedTracks[i]);
        }

        /// <summary>
        /// Saves the scheduled tracks to isolated storage, and uploads them to the service.
        /// </summary>
        public void Save()
        {
            // first, save to isolated storage
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(isolatedStorageFilename, FileMode.Create, isf))
                    using (StreamWriter sw = new StreamWriter(isfs))
                        for (int i = 0; i < Tracks.Length; i++)
                        {
                            if (i > 0) sw.Write(",");
                            sw.Write(Tracks[i]);
                        }

            //var proxy = new ServiceReference1.SessionServiceClient();
            ////proxy.UserScheduleUpdatedCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(
            ////    (sender, e) => /* do nothing here, for now at least */ );
            //proxy.UserScheduleUpdatedAsync("TestIpAddress", Tracks);
        }
    }
}
